#include "./../Server.hpp"


//实现一个简单的回显服务器类！
class EchoServer{
private:
    void OnConnected(ConnPtr conn){
        LOG(LogLevel::DEBUG) << "NEW CONNECTION : %s" << conn.get();
    }

    void OnMessage(ConnPtr conn, Buffer* buf){
        std::string s = buf->ReadAsString(buf->ReadableSize());
        LOG(LogLevel::DEBUG) << s;
        std::string ret = "echo : ";
        ret += s;
        conn->Send((char*)ret.c_str(), ret.size());
    }

    void OnClose(ConnPtr conn){
        LOG(LogLevel::DEBUG) << "CLOSE FD: " << conn->Fd();
    }

    void OnAny(ConnPtr conn){
        LOG(LogLevel::DEBUG) << "OnAny : " << conn->Fd();
    }

public:
    EchoServer(uint32_t port):_server(port)
    {
        /*设置回调*/
        _server.SetAnyEventCallback(std::bind(&EchoServer::OnAny, this, std::placeholders::_1));
        _server.SetClosedCallback(std::bind(&EchoServer::OnClose, this, std::placeholders::_1));
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));

        /*设置线程数量*/
        _server.SetLoopThreadNum(5);
        /*启动超时任务*/
        _server.EnableInactiveRelease(2);
    }

    ~EchoServer(){}

    void Start(){_server.Start();}
private:
    TcpServer _server;
};